<?php
/*
 Plugin Name: Product Manager
 Plugin URI: http://website-in-a-weekend.net/plugins/
 Description: Plugin for fngroup.vn site
 Version: 0.1
 Author: Binh Dang
 Author URI: http://website-in-a-weekend.net/plugins/
 */
 
/*  Copyright 2009  Dave Doolin  (email : david.doolin@gmail.com)
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */
 
/**
 * This plugin exists only to demonstrate _exactly_ how a database 
 * table is set up and torn down.
 */


	function custom_post_type_init() {
	  $labels = array(
		'name' => _x('product', 'post type general name'),
		'singular_name' => _x('product', 'post type singular name'),
		'add_new' => _x('Add New', 'Product'),
		'add_new_item' => __('Add New Product'),
		'edit_item' => __('Edit Product'),
		'new_item' => __('New Product'),
		'all_items' => __('All Product'),
		'view_item' => __('View Product'),
		'search_items' => __('Search Product'),
		'not_found' =>  __('No Product found'),
		'not_found_in_trash' => __('No Product found in Trash'), 
		'parent_item_colon' => '',
		'menu_name' => __('Product')

	  );
	  $args = array(
		'labels' => $labels,
		'public' => true,
		'publicly_queryable' => true,
		'show_ui' => true, 
		'show_in_menu' => true, 
		'query_var' => true,
		'rewrite' => true,
		'capability_type' => 'post',
		'has_archive' => true, 
		'hierarchical' => false,
		'menu_position' => null,
		'supports' => array( 'title', 'editor', 'thumbnail' )
	  ); 
	  register_post_type('product',$args);
	  
		 $labels = array(
		'name' => 'Service Type',
		'singular_name' => 'Service Type',
		'search_items' =>  __( 'Search Service Type' ),
		'all_items' => __( 'All Service Types' ),
		'parent_item' => __( 'Parent Service Type' ),
		'parent_item_colon' => __( 'Parent Service Type:' ),
		'edit_item' => __( 'Edit Service Type' ), 
		'update_item' => __( 'Update Service Type' ),
		'add_new_item' => __( 'Add New Service Type' ),
		'new_item_name' => __( 'New Service Type' ),
		'menu_name' => __( 'Service Type' ),
	  ); 	
	   register_taxonomy('service',array('product'), array(
		'hierarchical' => true,
		'labels' => $labels,
		'show_ui' => true,
		'query_var' => true,
		'rewrite' => array( 'slug' => 'Class' ),
	  ));
	}
	
	add_action( 'init', 'custom_post_type_init' );
	
	add_action("admin_init", "admin_init");   
	
	function admin_init(){
		add_meta_box("product-meta", "Product Information", "add_meta_box_product", "product", "normal", "low"); 	 
	}  
	 
	 function add_meta_box_product( $object, $box){ 
		//
		global $post; $custom = get_post_custom($post->ID); $product_completed = $custom["product-meta"][0];
		wp_nonce_field( basename( __FILE__ ), 'smashing_post_class_nonce' );
		 ?>
			<p>
				<label>Product Name</label>
				<br />
				<input class="widefat" type="text" name="product_name" value="<?php echo esc_attr( get_post_meta( $object->ID, 'product_name', true ) ); ?>" size="100" />
			</p>
			<p>
				<label>Product thumb image</label>
				<br />
				<input class="widefat" type="text" name="product_image"  value="<?php echo esc_attr( get_post_meta( $object->ID, 'product_image', true ) ); ?>" size="50" />
			</p>
			<p>
				<label >Short description</label>
				<br />				
				<textarea name="product_description" cols="46" rows="4">
				<?php echo esc_attr( get_post_meta( $object->ID, 'product_description', true ) ); ?>
				</textarea>
			</p>
			
			
			<p>
				
				<input  style="margin-left: 0 !important; width: 20px;" type="checkbox" name="special_product" <?php if(get_post_meta( $object->ID, 'special_product', true ) =="on") echo "checked" ?>  />	
				<label for="tour_featured">			
				Sepecial Product</label>			
				
			</p>	
			

		<?php		
	}
	
	add_action( 'save_post', 'post_product', 10, 2 );	
	

	/* Save the meta box's post metadata. */
	function post_product( $post_id, $post ) {

		/* Verify the nonce before proceeding. */
		if ( !isset( $_POST['smashing_post_class_nonce'] ) || !wp_verify_nonce( $_POST['smashing_post_class_nonce'], basename( __FILE__ ) ) )
			return $post_id;

		/* Get the post type object. */
		$post_type = get_post_type_object( $post->post_type );

		/* Check if the current user has permission to edit the post. */
		if ( !current_user_can( $post_type->cap->edit_post, $post_id ) )
			return $post_id;

		/* Get the posted data and sanitize it for use as an HTML class. */
		$product_name = ( isset( $_POST['product_name'] ) ?  $_POST['product_name'] : '' );
		$product_image = ( isset( $_POST['product_image'] ) ?  $_POST['product_image']  : '' );		
		$special_product = ( isset( $_POST['special_product'] ) ?  $_POST['special_product']  : '' );
		$product_description = ( isset( $_POST['product_description'] ) ? $_POST['product_description']: '' );
		/* Get the meta key. */
		$key_product_name = 'product_name';
		$key_product_image = 'product_image';
		$key_special_product = 'special_product';
		$key_product_description = 'product_description';
		/* Get the meta value of the custom field key. */
		$old_product_name = get_post_meta( $post_id, $key_product_name, true );
		$old_product_image = get_post_meta( $post_id, $key_product_image, true );
		$old_special_product = get_post_meta( $post_id, $key_special_product, true );
		$old_product_description = get_post_meta( $post_id, $key_product_description, true );
		
		/* === Product name === */
		if ( $product_name && '' == $old_product_name )
			add_post_meta( $post_id, $key_product_name, $product_name, true );	
		elseif ( $product_name && $product_name != $old_product_name )
			update_post_meta( $post_id, $key_product_name, $product_name );	
		elseif ( '' == $product_name && $old_product_name )
			delete_post_meta( $post_id, $key_product_name, $product_name );
		
			
		/* === Product image === */
		if ( $product_image && '' == $old_product_image )
			add_post_meta( $post_id, $key_product_image, $product_image, true );
		elseif ( $product_image && $product_image != $old_product_image )
			update_post_meta( $post_id, $key_product_image, $product_image );
		elseif ( '' == $product_image && $old_product_image )
			delete_post_meta( $post_id, $key_product_image, $product_image );
			
	
		/* === Special product === */	
		if ( $special_product && '' == $old_special_product )
			add_post_meta( $post_id, $key_special_product, $special_product, true );
		elseif ( $special_product && $special_product != $old_special_product )
			update_post_meta( $post_id, $key_special_product, $special_product );
		elseif ( '' == $special_product && $old_special_product )
			delete_post_meta( $post_id, $key_special_product, $special_product );	
		
		/* === product description === */	
		if ( $product_description && '' == $old_product_description )
			add_post_meta( $post_id, $key_product_description, $product_description, true );
		elseif ( $product_description && $product_description != $old_product_description )
			update_post_meta( $post_id, $key_product_description, $product_description );
		elseif ( '' == $product_description && $old_product_description )
			delete_post_meta( $post_id, $key_product_description, $special_product );	
	}
	
